package com.example.register.controller;

import com.example.base.entity.User;
import com.example.base.entity.UserInfo;
import com.example.base.service.UserService;
import com.example.base.utils.Md5Generator;
import com.example.base.vo.RespBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.UUID;


@Controller
public class RegisterController {
    @Autowired
    UserService userService;

    @PostMapping("/register")
    @ResponseBody
    public RespBean register(@RequestBody @Validated User user) {
        user.setPassWord(Md5Generator.getMD5String(user.getPassWord()));
        UserInfo info = user.getUserInfo();
        userService.insertUserInfo(info);
        userService.insertUser(user, info.getId());
        String uuid = UUID.randomUUID().toString();
        userService.insertUserCode(info.getId(), uuid);

        //使用redis缓存用户所有信息，user_info, code
        userService.cacheUser(user);
        userService.cacheUserCode(info.getId(), uuid);
        userService.cacheUserInfoById(info.getId(), info);
        System.out.println("uuid = " + uuid);
        return RespBean.success();
    }
}
